package com.thon.controller.rest;

import com.thon.commons.persistence.Page;
import com.thon.controller.util.BaseController;
import com.thon.entity.college.College;
import com.thon.entity.test.Details;
import com.thon.entity.test.Report;
import com.thon.service.test.DetailsService;
import com.thon.service.test.ReportService;
import com.thon.service.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by win on 2016/1/7.
 */
@Controller
@RequestMapping("/api/report")
public class TestRestController extends BaseController {

    @Autowired
    private ReportService reportService;
    @Autowired
    private DetailsService detailsService;

    //添加/修改填报表
    @RequestMapping(value="/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiReportAdd(@Valid Report report) {
        report.setCreateDate(new Date());
        reportService.saveReport(report);
        return new ResponseEntity(true, HttpStatus.OK);
    }

    //删除填报表
    @RequestMapping(value="/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiReportDel(@RequestParam("id") int id) {
        if(UserUtils.getUser().isAdmin()){
            Report report = reportService.getReport(id);
            reportService.deleteReport(report);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //分页+条件查询所有填报表
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseEntity<?> list(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                  @RequestParam(value = "orderName", defaultValue = "id") String orderName,
                                  @RequestParam(value = "order", defaultValue = "desc") String order,
                                  Report report) {

        Page<Report> page = new Page<Report>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page = reportService.findReports(page,report,orderName,order);
        return new ResponseEntity(page, HttpStatus.OK);
    }

    //添加/修改填报详情表
    @RequestMapping(value="/details/save", method = RequestMethod.POST)
    public ResponseEntity<?> apiDetailsAdd(@Valid Details details) {
        detailsService.saveDetails(details);
        return new ResponseEntity(true, HttpStatus.OK);
    }

    //删除填报详情表
    @RequestMapping(value="/details/del", method = RequestMethod.POST)
    public ResponseEntity<?> apiDetailsDel(@RequestParam("id") int id) {
        if(UserUtils.getUser().isAdmin()){
            Details details = detailsService.getDetails(id);
            detailsService.deleteDetails(details);
            return new ResponseEntity("true", HttpStatus.OK);
        }else {
            return new ResponseEntity("false", HttpStatus.FORBIDDEN);
        }
    }

    //分页+条件查询所有填报详情表
    @RequestMapping(value = "/details/list", method = RequestMethod.GET)
    public ResponseEntity<?> list(@RequestParam(value = "p", defaultValue = "1") int pageNo,
                                  @RequestParam(value = "s", defaultValue = PAGE_SIZE) int pageSize,
                                  @RequestParam(value = "orderName", defaultValue = "id") String orderName,
                                  @RequestParam(value = "order", defaultValue = "desc") String order,
                                  Details details) {

        Page<Details> page = new Page<Details>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page = detailsService.findDetails(page,details,orderName, order);

        return new ResponseEntity(page, HttpStatus.OK);
    }


}
